<?php
$this->Html->addCrumb('Schedule','/Schedules');
$this->Html->addCrumb('Job Details');
function findage($dob)
{
    $localtime = getdate();
    $today = $localtime['mday']."-".$localtime['mon']."-".$localtime['year'];
    $dob_a = explode("-", $dob);
    $today_a = explode("-", $today);
    $dob_d = $dob_a[0];
	$dob_m = $dob_a[1];
	$dob_y = $dob_a[2];
    $today_d = $today_a[0];$today_m = $today_a[1];$today_y = $today_a[2];
    $years = $today_y - $dob_y;
    $months = $today_m - $dob_m;
    if ($today_m.$today_d < $dob_m.$dob_d) 
    {
        $years--;
        $months = 12 + $today_m - $dob_m;
    }

    if ($today_d < $dob_d) 
    {
        $months--;
    }

    $firstMonths=array(1,3,5,7,8,10,12);
    $secondMonths=array(4,6,9,11);
    $thirdMonths=array(2);

    if($today_m - $dob_m == 1) 
    {
        if(in_array($dob_m, $firstMonths)) 
        {
            array_push($firstMonths, 0);
        }
        elseif(in_array($dob_m, $secondMonths)) 
        {
            array_push($secondMonths, 0);
        }elseif(in_array($dob_m, $thirdMonths)) 
        {
            array_push($thirdMonths, 0);
        }
    }
    return "$years Years $months months.";
}
//debug($routes);
?>

<div id=job>
	<div id="jobDetails">
		<h3>Job Details/ Request</h3>
		<table class="table">
			<tbody>
				<?php
				foreach($jobs as $job)
				{
						echo '<tr><th>Job ID</th><td>'.$job[0]['JobId'].'</td></tr>';
						echo '<tr><th>Job Name</th><td>'.substr($job[0]['JobName'], 2, -1).'</td></tr>';
						echo '<tr><th>Job Type</th><td>'.$job[0]['JobType'].'</td></tr>';
						echo '<tr><th>Request Version</th><td>'.$job[0]['RequestVersion'].'</td></tr>';
						echo '<tr><th>Description</th><td>'.$job[0]['Description'].'</td></tr>';
						echo '<tr><th>Start Date</th><td>'.date_format(date_create_from_format('Y-m-d H:i:s.u', $job[0]['StartDate']), 'd-m-y').'</td></tr>';
						echo '<tr><th>End Date</th><td>'.date_format(date_create_from_format('Y-m-d H:i:s.u', $job[0]['EndDate']), 'd-m-y').'</td></tr>';
						
						
				}
			?>
			</tbody>
		</table>
	</div>

	<div id="route">
		<h3>Route</h3>
		<?php
		foreach($staffs as $key => $staff)
		{
		echo '<h4>'.substr($staff[0]['FirstName'], 2, -1).' '.substr($staff[0]['Surname'], 2, -1).'</h4>';
		?>
				<?php 
				$worker = preg_replace('/[^\p{L}\p{N}\s]/u', '', $staff[0]['FirstName']);
				//debug($$worker);
				foreach($$worker as $contactWorker)
				{
					echo '<div id="routeRow">';
						echo '<div id="routeSeqBlock">';
							echo '<span id="seq">'.$contactWorker[0]['RoutePointSequence'].'</span>';
						echo '</div>';
						echo '<div id="infoBlock">';
							echo '<span id="routeName">'.$contactWorker[0]['RouteName'].'</span>';
							
							$address = "";
							if($contactWorker[0]['LevelNumber']!= '')
							$address .= $contactWorker[0]['LevelNumber'].'- ';
							if($contactWorker[0]['UnitNumber']!= '')
							$address .= $contactWorker[0]['UnitNumber'].'/ ';
							if($contactWorker[0]['StreetNumber']!= '')
							$address .= $contactWorker[0]['StreetNumber'].' ';
							
							
							$address .= $contactWorker[0]['StreetName'].' '.$contactWorker[0]['StreetSuffix'].' '.$contactWorker[0]['TownSuburb'].' '.$contactWorker[0]['State'].' '.$contactWorker[0]['PostCode'].' '.$contactWorker[0]['Country'];
							
							echo '<span id="address">'.substr($address, 5, -5).'</span>';
							if($contactWorker[0]['ContactLocationFlag'] == 'Y')
							{ $contactFlag = 'Yes'; }
							else
							{ $contactFlag = 'No'; }
							echo '<span id="contactLocationHeader">Contact Location: </span><span id="contactLocation">'.$contactFlag.'</span>';
							
							echo '<div id="notes"><span id="notesHeader">Notes:</span><span id="notesText">'.$contactWorker[0]['Note'].'</span></div>';
							
						echo '</div>';
						echo '<div id="timeBlock">';
							$arrivalTime = strtotime(str_replace( '.', '/', $contactWorker[0]['ExpectedArrivalTime']));
							echo '<span id="ExpectedArrivalTime">'.date_format(date_create_from_format('Y-m-d H:i:s.u', $contactWorker[0]['ExpectedArrivalTime']), 'H:i').'</span>';
							echo '<span id="ExpectedDepartureTime"> to '.date_format(date_create_from_format('Y-m-d H:i:s.u', $contactWorker[0]['ExpectedDepartureTime']), 'H:i').'</span>';
						echo '</div>';
					echo '</div>';
					
				}
				
				
				?>

		<?php
		}
		?>
	</div>
	<div id="clients">
		<h3>Clients</h3>
		<table class="table">
			<thead>
				<th>Name</th>
				<th>Age</th>
				<th>Booster Seat</th>
			</thead>
			<tbody>
				<?php foreach($clients as $client)
				{
					echo '<tr>';
						echo '<td>'.substr($client[0]['FirstName'], 2, -1).' '.substr($client[0]['Surname'], 2, -1).'</td>';
						echo '<td>'.findAge(date('d-m-Y', strtotime($client[0]['DOB']))).'</td>';
						if(isset($client[0]['RequiresRFCarSeatBefore']))
						{
							echo'<td>Requires Rear Facing Car Seat</td>';
						}
						else if(isset($client[0]['RequiresFFCarSeatBefore']))
						{
							echo'<td>Requires Front Facing Car Seat</td>';
						}
						else if(isset($client[0]['RequiresBoosterSeatBefore']))
						{
							echo'<td>Requires Booster Seat</td>';
						}
					echo '</tr>';
				}
				?>
			</tbody>
		</table>
	</div>
	<div id="notes">
		<h3>Notes</h3>
		<table class="table">
			<thead>
				<th>Importance</th>
				<th>Note</th>
				<th>Created</th>
			</thead>
			<tbody>
				<?php foreach($notes as $note)
				{
					echo '<tr>';
						echo '<td>'.$note[0]['Importance'].'</td>';
						echo '<td>'.substr($note[0]['NoteText'], 10, -10).'</td>';
						echo '<td>'.$note[0]['CreatedOn'].'</td>';
					echo '</tr>';
				}
				
				?>
				
			</tbody>
		</table>
	</div>
</div>